<template>
    <div class="page-section">
            <div class="page-tool">
                <el-button type="primary"  plain icon="el-icon-plus" @click="add" size="small">新增</el-button>
            </div>
            <!--表单-->
            <el-table :data="CurrQcReceiveStandardModel.Children"  ref="dataForm"   v-loading="isLoading"  height="300">
                <el-table-column prop="LevelName" label="缺陷名称" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                         <el-select :value="scope.row.LevelName" placeholder="请选择常见缺陷" @change="qcProjectLevelChange(scope.row,$event)">
                            <el-option v-for="item in QcProjectLevelList" :key="item.Id" :label="item.LevelName" :value="item"></el-option>
                        </el-select>
                    </template>
                </el-table-column>
                <el-table-column  prop="Count" label="缺陷数量" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.Count"  @change="handleBlur(scope.row)" type="number" oninput="value=value.replace(/[^0-9.]/g,'')"/>
                    </template>
                </el-table-column>

                <el-table-column  prop="Remark" label="描述" :show-overflow-tooltip="true">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.Remark"/>
                    </template>
                </el-table-column>
                
                <el-table-column   label="操作" :show-overflow-tooltip="true" width="160">
                    <template slot-scope="scope">
                        <el-button type="text" @click="del(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <div style="text-align:right; margin: 10px;">
                <el-button type="primary" @click="submitForm">保存</el-button>
            </div>
    </div>
</template>

<script>
export default {

    props:{
            model: {
                require: true,
                type: Object
            },
            CurrQcReceiveStandardModel: {
                require: true,
                type: Object
            },
    },
    //实例生命周期
    created() {
        this.getQcProjectLevelData();
    },
    data(){
        return {
            isLoading:false,
            //常见缺陷
            QcProjectLevelList:[],
 

        }
    },
    methods:{
        add(){
            
 
            if(this.CurrQcReceiveStandardModel.Children.length+1 <= parseInt(this.model.CheckNumber)){


                this.CurrQcReceiveStandardModel.Children.push({
                    QcProjectLevel:'',
                    LevelName:'',
                    Count:'0',
                    Remark:'',
                });
            }
            else{
                this.showNotify('缺陷数量不能大于抽检数量');
            }
           


            
        },

        del(model) {
            let index = this.CurrQcReceiveStandardModel.Children.indexOf(model);
            this.CurrQcReceiveStandardModel.Children.splice(index, 1);
        },
        //获取常见缺陷
        getQcProjectLevelData() {
            this.isLoading = true;
            this.$http.get('/api/QcProjectLevel/GetAll')
                .then(res => {
                    this.QcProjectLevelList=res.Data;
                    
                    this.isLoading = false;
                })
        },
        //选择仓库时为其添加仓库名和Id
        qcProjectLevelChange(row,model){
            row.QcProjectLevel=model.Id;
            row.LevelName=model.LevelName;
        },
        handleBlur(row){
            
            console.log(this.model.CheckNumber);
            if(this.model.CheckNumber==0){
                this.showNotify('抽检数不能等于0');
                row.Count = 0
                return;
            }
            if (row.Count === 0) {
                this.showNotify('请输入缺陷数量');
            }


            let leg = 0;
            this.CurrQcReceiveStandardModel.Children.map((item)=>{
                leg+=parseInt(item.Count);
            }) 
            if (leg >  this.model.CheckNumber) {
                this.showNotify('缺陷数量不能大于抽检数量');
                row.Count = 0
            }

        },
        submitForm(){
             try{
                    this.CurrQcReceiveStandardModel.Children.map((item)=>{
                       if(parseInt(item.Count)==0){
                        throw item.LevelName+'数量不能为0';
                       }
                    }) 
                    this.$emit('callback');
                    
                    let leg = 0;
                    this.CurrQcReceiveStandardModel.Children.map((item)=>{
                        leg+=parseInt(item.Count);
                    }) 
                    this.model.Unqualified=leg;
             }
             catch(e){
                this.showNotify(e);
             }
        },
    }

}
</script>

<style>

</style>